<template>
    <el-card shadow="always" :body-style="{ padding: '20px' }">
        <el-form :inline="true" class="demo-form-inline" :disabled="disables">
           
            <el-form-item label="一级分类">
                <el-select  placeholder="请选择" v-model="category1Id" @change="changeCategory1">
                    <el-option 
                        v-for="item in category1List"
                        :key="item.id"
                        :label="item.name" 
                        :value="item.id" 
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="二级分类">
                <el-select  placeholder="请选择" v-model="category2Id" @change="changeCategory2">
                    <el-option 
                        v-for="item in category2List"
                        :key="item.id"
                        :label="item.name" 
                        :value="item.id" 
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="三级分类">
                <el-select  placeholder="请选择" v-model="category3Id" >
                    <el-option 
                        v-for="item in category3List"
                        :key="item.id"
                        :label="item.name" 
                        :value="item.id" 
                    />
                </el-select>
            </el-form-item>
            
        </el-form>
    </el-card>
    
</template>

<script lang="ts">
import { defineComponent } from "vue";
import { storeToRefs } from "pinia";

export default defineComponent({
    name: "Category",
});
</script>

<script setup lang="ts">
import {onMounted} from "vue"
import useAttrStore from "@/stores/attr"
const attrStore = useAttrStore()

defineProps<{
    disables: boolean
}>()

//解构出attr仓库中的数据
const {
    category1List,
    category2List,
    category3List,
    category1Id,
    category2Id,
    category3Id,
} = storeToRefs(attrStore)

//1.初始化请求一级列表
onMounted(()=>{
    attrStore.getCategory1()
})

//2.一级列表改变的回调
const changeCategory1 = () => {
    //点击一级裂变之前清空二级，三级列表的数据
    attrStore.category2Id = null
    // attrStore.category2List = []
    attrStore.category3Id = null
    attrStore.category3List = []
    //拿着改变后的category1Id请求二级列表
    attrStore.getCategory2()
}

//3.三级列表改变的回调
const changeCategory2 = () => {
    //点击二级列表之前清空三级列表的数据
    attrStore.category3Id = null
    // attrStore.category3List = []
    //拿着改变后的category2Id请求三级列表
    attrStore.getCategory3()
}

</script>